<?php

namespace App\Providers;

use App\Models\Company\CompanyUser;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
use App\Models\Personal\PersonalUser;
use Validator, CustomPassword;

class UserLoginProvider extends ServiceProvider {
    public function boot () {
        //自定义手机验证规则改写
        Validator::extend('isset_name', function ($attribute, $value, $parameters) {
            $personalUser = PersonalUser::where('username', $value)->orWhere('mobile', $value)->get();
            if (count($personalUser)) {
                return true;
            } else {
                return false;
            }
        });
        Validator::extend('check_username', function ($attribute, $value, $parameters) {
            $personalUser = PersonalUser::where('username', $parameters[0])->orWhere('mobile', $parameters[0])->first(['password']);
            if (count($personalUser)) {
                if (CustomPassword::check($value, $personalUser->password)) {
                    return true;
                } else {
                    return false;
                }
                //return Auth::validateCredentials($personalUser,$parameters);
            }
        });

        //自定义个人证规则改写
        Validator::extend('isset_username', function ($attribute, $value, $parameters) {
            $uid = $parameters[0];
            $personalUser = PersonalUser::where('uid', '!=', $uid)->where('username', $value)->orWhere('mobile', $value)->get();
            if (count($personalUser)) {
                return false;
            } else {
                return true;
            }
        });

        //自定义企业证规则改写
        Validator::extend('isset_company_username', function ($attribute, $value, $parameters) {
            $uid = $parameters[0];
            $companyUser = CompanyUser::where('uid', '!=', $uid)->where('username', $value)->get();
            if (count($companyUser)) {
                return false;
            } else {
                return true;
            }
        });

    }

    /**
     * Register the application services.
     *
     * @return void
     */

    public function register () {

    }
}
